<?php
/**
 * User: krisbuist
 * Date: 2/2/12
 * Time: 11:21 AM
 */

require_once APPPATH . 'core/controller.php';
require_once APPPATH . 'models/searchResult.php';
require_once APPPATH . 'models/titleSearch.php';
require_once APPPATH . 'models/tagSearch.php';
require_once APPPATH . 'models/authorSearch.php';
require_once APPPATH . 'models/mediaSearch.php';

class SearchController extends Controller
{
    public function quickSearch()
    {
        $searchString = $this->input->get_post('searchField', true);
        if (empty($searchString)) {
            $this->advancedSearchForm();
        }
        else
        {
            $this->standardSearch($searchString);
        }
    }

    protected $numberOfSearchResultsPerPage = 10;

    public function standardSearch($searchString)
    {
        $titleSearch = new TitleSearch($this->em, $searchString);
        $this->data['titleSearch'] = $titleSearch->getResults(1);
        $this->data['titleNumberOfResults'] = $titleSearch->getTotalNumberOfResults();
        $this->data['titleNumberOfPages'] = $titleSearch->getTotalNumberOfPages();

        $tagSearch = new TagSearch($this->em, $searchString);
        $this->data['tagSearch'] = $tagSearch->getResults(1);
        $this->data['tagNumberOfResults'] = $tagSearch->getTotalNumberOfResults();
        $this->data['tagNumberOfPages'] = $tagSearch->getTotalNumberOfPages();

        $authorSearch = new AuthorSearch($this->em, $searchString);
        $this->data['authorSearch'] = $authorSearch->getResults(1);
        $this->data['authorNumberOfResults'] = $authorSearch->getTotalNumberOfResults();
        $this->data['authorNumberOfPages'] = $authorSearch->getTotalNumberOfPages();

        $mediaSearch = new MediaSearch($this->em, $searchString);
        $this->data['mediaSearch'] = $mediaSearch->getResults(1);
        $this->data['mediaNumberOfResults'] = $mediaSearch->getTotalNumberOfResults();
        $this->data['mediaNumberOfPages'] = $mediaSearch->getTotalNumberOfPages();

        $this->data['searchString'] = $searchString;

        $this->data['styles'][] = '/public/css/search.css';
        $this->parser->parse('search/quick.tpl', $this->data);
    }

    public function titlePaginated($searchString, $pageNumber)
    {
        $titleSearch = new TitleSearch($this->em, $searchString);
        $this->data['titleSearch'] = $titleSearch->getResults($pageNumber);

        $this->parser->parse('search/partials/tableData/titleSearchTable.tpl', $this->data);
    }

    public function tagPaginated($searchString, $pageNumber)
    {
        $tagSearch = new TagSearch($this->em, $searchString);
        $this->data['tagSearch'] = $tagSearch->getResults($pageNumber);

        $this->parser->parse('search/partials/tableData/tagSearchTable.tpl', $this->data);
    }

    public function authorPaginated($searchString, $pageNumber)
    {
        $authorSearch = new AuthorSearch($this->em, $searchString);
        $this->data['authorSearch'] = $authorSearch->getResults($pageNumber);

        $this->parser->parse('search/partials/tableData/authorSearchTable.tpl', $this->data);
    }

    public function mediaPaginated($searchString, $pageNumber)
    {
        $mediaSearch = new MediaSearch($this->em, $searchString);
        $this->data['mediaSearch'] = $mediaSearch->getResults($pageNumber);

        $this->parser->parse('search/partials/tableData/mediaSearchTable.tpl', $this->data);
    }
}
